package com.gitee.wsl.android.eventbus

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.lifecycle.asLiveData
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import java.util.concurrent.ConcurrentHashMap

object SweetFlowBus {
    private var events = ConcurrentHashMap<Any, MutableSharedFlow<Any>>()
    private var stickyEvents = ConcurrentHashMap<Any, MutableSharedFlow<Any>>()

    private fun <T> with(objectKey: Any): MutableSharedFlow<T> {
       if (!events.containsKey(objectKey)) {
            events[objectKey] = MutableSharedFlow(0, 1, BufferOverflow.DROP_OLDEST)
        }
        return events[objectKey] as MutableSharedFlow<T>
    }

    private fun <T> withSticky(objectKey: Any): MutableSharedFlow<T> {
        if (!stickyEvents.containsKey(objectKey)) {
            stickyEvents[objectKey] = MutableSharedFlow(1, 1, BufferOverflow.DROP_OLDEST)
        }
        return stickyEvents[objectKey] as MutableSharedFlow<T>
    }

    fun <T> observe(owner: LifecycleOwner, objectKey: Any, observer: Observer<T>) {
        return with<T>(objectKey).asLiveData().observe(owner, observer)
    }

    fun observe(owner: LifecycleOwner, objectKey: Any, observer: () -> Unit) {
        return with<EmptyMessage>(objectKey).asLiveData().observe(owner) {
            observer()
        }
    }

    fun <T> observeForever(objectKey: Any, observer: Observer<T>) {
        return with<T>(objectKey).asLiveData().observeForever(observer)
    }

    fun observeForever(objectKey: Any, observer: () -> Unit) {
        return with<EmptyMessage>(objectKey).asLiveData().observeForever {
            observer()
        }
    }

    fun <T> observeSticky(owner: LifecycleOwner, objectKey: Any, observer: Observer<T>) {
        return withSticky<T>(objectKey).asLiveData().observe(owner, observer)
    }

    fun observeSticky(owner: LifecycleOwner, objectKey: Any, observer: () -> Unit) {
        return withSticky<EmptyMessage>(objectKey).asLiveData().observe(owner) {
            observer()
        }
    }

    fun <T> observerStickyForever(objectKey: Any, observer: Observer<T>) {
        return withSticky<T>(objectKey).asLiveData().observeForever(observer)
    }

    fun observerStickyForever(objectKey: Any, observer: () -> Unit) {
        return withSticky<EmptyMessage>(objectKey).asLiveData().observeForever {
            observer()
        }
    }

    fun post(objectKey: Any) {
        with<EmptyMessage>(objectKey).tryEmit(EmptyMessage)
    }

    fun <T> post(objectKey: Any, objectValue: T) {
        with<T>(objectKey).tryEmit(objectValue)
    }

    fun postSticky(objectKey: Any) {
        withSticky<EmptyMessage>(objectKey).tryEmit(EmptyMessage)
    }

    fun <T> postSticky(objectKey: Any, objectValue: T) {
        withSticky<T>(objectKey).tryEmit(objectValue)
    }

    fun <T> getEventFlow(objectKey: Any): Flow<T> = with(objectKey)

    fun isExits(key: String): Boolean = events.containsKey(key)
}

object EmptyMessage{}